<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>家庭书籍管理系统</title>
<link rel="stylesheet" href="css/thin_table.css">
</head>
<body onload="onLoadBookShelf(0);">
<table width="100%" border="0">
  <tr>
    <td>书架管理 &gt;&gt;&gt;</td>
  </tr>
</table>
<br />
<table class="thin_table">
	<tr class="thin_table_tr">
		<td><div align="right">书架ID：</div></td>
		<td id="labelBookShelfId">&nbsp;</td>
		<td>&nbsp;</td>
		<td rowspan="2"><div align="center">
			<input type="button" name="buttonAddBookShelf" id="buttonAddBookShelf" value="   新增书架   " onclick="onAddBookShelf(0);" />
			<input type="button" name="buttonModifyBookShelf" id="buttonModifyBookShelf" value="   修改书架   " onclick="onModifyBookShelf(0);" />
			<input type="button" name="buttonRemoveBookShelf" id="buttonRemoveBookShelf" value="   删除书架   " onclick="onRemoveBookShelf();" />
		</div></td>
	</tr>
	<tr class="thin_table_tr">
		<td><div align="right">书架描述：</div></td>
		<td><label>
			<select name="selectBookShelf" id="selectBookShelf" onchange="onChangeBookShelf();">
			</select>
		</label></td>
		<td><input name="textfieldBookShelfDesc" type="text" id="textfieldBookShelfDesc" size="32" maxlength="32" /></td>
	</tr>
	<tr class="thin_table_tr">
		<td><div align="right">书架层号：</div></td>
		<td id="labelBookShelfLayerNumber">&nbsp;</td>
		<td><input name="textfieldBookShelfLayerNumber" type="text" id="textfieldBookShelfLayerNumber" size="10" maxlength="10" /></td>
		<td rowspan="2"><div align="center">
			<input type="button" name="buttonAddBookShelfLayer" id="buttonAddBookShelfLayer" value="   新增书架层   " onclick="onAddBookShelfLayer();" />
			<input type="button" name="buttonModifyBookShelfLayer" id="buttonModifyBookShelfLayer" value="   修改书架层   " onclick="onModifyBookShelfLayer();" />
			<input type="button" name="buttonRemoveBookShelfLayer" id="buttonRemoveBookShelfLayer" value="   删除书架层   " onclick="onRemoveBookShelfLayer();" />
		</div></td>
	</tr>
	<tr class="thin_table_tr">
		<td><div align="right">书架层描述：</div></td>
		<td><select name="selectBookShelfLayer" id="selectBookShelfLayer" onchange="onChangeBookShelfLayer();">
		</select></td>
		<td><input name="textfieldBookShelfLayerDesc" type="text" id="textfieldBookShelfLayerDesc" size="32" maxlength="32" /></td>
	</tr>
</table>
<script language="javascript">
	function onLoadBookShelf(book_shelf_id) {
		var selectBookShelf = document.getElementById( 'selectBookShelf' ) ;
		/*
		const onchangeHandlerDefault_BookShelf = selectBookShelf.onchange ;
		selectBookShelf.onchange = null ;
		var selectBookShelfLayer = document.getElementById( 'selectBookShelfLayer' ) ;
		const onchangeHandlerDefault_BookShelfLayer = selectBookShelfLayer.onchange ;
		selectBookShelfLayer.onchange = null ;
		*/
		
		var xhr = new XMLHttpRequest() ;
		xhr.open( 'GET' , '/book_shelf_data.zweb' , true );
		xhr.responseType = 'json' ;
		xhr.onreadystatechange = function() {
			if( xhr.readyState == 4 ) {
				if( xhr.status == 200 ) {
					selectBookShelf.innerHTML = '' ;
					xhr.response.forEach( function(item) {
						var option = document.createElement('option') ;
						option.value = item.book_shelf_id ;
						option.textContent = item.book_shelf_desc ;
						selectBookShelf.appendChild(option);
						if( book_shelf_id > 0 && option.value == book_shelf_id )
							selectBookShelf.value = option.value ;
					});
					
					onChangeBookShelf(0);
					
					/*
					selectBookShelf.onchange = onchangeHandlerDefault_BookShelf ;
					selectBookShelfLayer.onchange = onchangeHandlerDefault_BookShelfLayer ;
					*/
				} else if( xhr.status == 204 ) {
					selectBookShelf.innerHTML = '' ;
					document.getElementById( 'labelBookShelfId' ).textContent = '' ;
					document.getElementById( 'textfieldBookShelfLayerNumber' ).value = '' ;
					document.getElementById( 'textfieldBookShelfLayerDesc' ).value = '' ;
				} else if( xhr.status == 500 ) {
					alert( xhr.response );
				}
			}
		}
		xhr.onerror = function() {
			alert( 'Network error' );
		}
		xhr.send();
	}
	function onChangeBookShelf(book_shelf_layer_number) {
		var selectBookShelf = document.getElementById( 'selectBookShelf' ) ;
		var textfieldBookShelfDesc = document.getElementById( 'textfieldBookShelfDesc' ) ;
		textfieldBookShelfDesc.value = selectBookShelf.options[selectBookShelf.selectedIndex].text ;
		var labelBookShelfId = document.getElementById( 'labelBookShelfId' ) ;
		labelBookShelfId.textContent = selectBookShelf.value ;
		var selectBookShelfLayer = document.getElementById( 'selectBookShelfLayer' ) ;
		
		var xhr = new XMLHttpRequest() ;
		xhr.open( 'GET' , '/book_shelf_layer_data.zweb?book_shelf_id='+selectBookShelf.value , true );
		xhr.responseType = 'json' ;
		xhr.onreadystatechange = function() {
			if( xhr.readyState == 4 ) {
				if( xhr.status == 200 ) {
					selectBookShelfLayer.innerHTML = '' ;
					xhr.response.forEach( function(item) {
						var option = document.createElement('option') ;
						option.value = item.book_shelf_layer_number ;
						option.textContent = item.book_shelf_layer_desc ;
						selectBookShelfLayer.appendChild(option);
						if( book_shelf_layer_number > 0 && option.value == book_shelf_layer_number )
							selectBookShelfLayer.value = option.value ;
					});
					
					onChangeBookShelfLayer();
				} else if( xhr.status == 204 ) {
					selectBookShelfLayer.innerHTML = '' ;
					document.getElementById( 'labelBookShelfLayerNumber' ).textContent = '' ;
					document.getElementById( 'textfieldBookShelfLayerNumber' ).value = '' ;
					document.getElementById( 'textfieldBookShelfLayerDesc' ).value = '' ;
				} else if( xhr.status == 500 ) {
					alert( xhr.response );
				}
			}
		}
		xhr.onerror = function() {
			alert( 'Network error' );
		}
		xhr.send();
	}
	function onChangeBookShelfLayer() {
		var selectBookShelfLayer = document.getElementById( 'selectBookShelfLayer' ) ;
		var textfieldBookShelfLayerDesc = document.getElementById( 'textfieldBookShelfLayerDesc' ) ;
		textfieldBookShelfLayerDesc.value = selectBookShelfLayer.options[selectBookShelfLayer.selectedIndex].text ;
		var labelBookShelfLayerNumber = document.getElementById( 'labelBookShelfLayerNumber' ) ;
		labelBookShelfLayerNumber.textContent = selectBookShelfLayer.value ;
		var textfieldBookShelfLayerNumber = document.getElementById( 'textfieldBookShelfLayerNumber' ) ;
		textfieldBookShelfLayerNumber.value = selectBookShelfLayer.value ;
	}
	function onAddBookShelf() {
		var textfieldBookShelfDesc = document.getElementById( 'textfieldBookShelfDesc' ) ;
		var uri = "/book_shelf_adding.zweb?book_shelf_desc="+encodeURIComponent(textfieldBookShelfDesc.value) ;
		
		var xhr = new XMLHttpRequest() ;
		xhr.open( 'POST' , uri , true );
		xhr.responseType = 'text' ;
		xhr.onreadystatechange = function() {
			if( xhr.readyState == 4 && xhr.status == 200 ) {
				if( xhr.response == '-1' )
					alert('失败');
				else
					onLoadBookShelf(xhr.response);
			}
		}
		xhr.onerror = function() {
			alert( 'Network error' );
		}
		xhr.send();
	}
	function onModifyBookShelf() {
		var labelBookShelfId = document.getElementById( 'labelBookShelfId' ) ;
		var textfieldBookShelfDesc = document.getElementById( 'textfieldBookShelfDesc' ) ;
		var uri = "/book_shelf_modifying.zweb?book_shelf_id="+labelBookShelfId.textContent+"&book_shelf_desc="+encodeURIComponent(textfieldBookShelfDesc.value) ;
		
		var xhr = new XMLHttpRequest() ;
		xhr.open( 'POST' , uri , true );
		xhr.responseType = 'text' ;
		xhr.onreadystatechange = function() {
			if( xhr.readyState == 4 && xhr.status == 200 ) {
				if( xhr.response == '-1' )
					alert('失败');
				else
					onLoadBookShelf(xhr.response);
			}
		}
		xhr.onerror = function() {
			alert( 'Network error' );
		}
		xhr.send();
	}
	function onRemoveBookShelf() {
		if( ! confirm('确认删除该书层信息？') )
			return;
		
		var labelBookShelfId = document.getElementById( 'labelBookShelfId' ) ;
		var uri = "/book_shelf_removing.zweb?book_shelf_id="+labelBookShelfId.textContent ;
		
		var xhr = new XMLHttpRequest() ;
		xhr.open( 'POST' , uri , true );
		xhr.responseType = 'text' ;
		xhr.onreadystatechange = function() {
			if( xhr.readyState == 4 && xhr.status == 200 ) {
				if( xhr.response == '-1' )
					alert('失败');
				else
					onLoadBookShelf(0);
			}
		}
		xhr.onerror = function() {
			alert( 'Network error' );
		}
		xhr.send();
	}
	function onAddBookShelfLayer() {
		var labelBookShelfId = document.getElementById( 'labelBookShelfId' ) ;
		var textfieldBookShelfLayerNumber = document.getElementById( 'textfieldBookShelfLayerNumber' ) ;
		var textfieldBookShelfLayerDesc = document.getElementById( 'textfieldBookShelfLayerDesc' ) ;
		var uri = '/book_shelf_layer_adding.zweb?book_shelf_id='+labelBookShelfId.textContent+'&book_shelf_layer_number='+textfieldBookShelfLayerNumber.value+'&book_shelf_layer_desc='+encodeURIComponent(textfieldBookShelfLayerDesc.value) ;
		
		var xhr = new XMLHttpRequest() ;
		xhr.open( 'POST' , uri , true );
		xhr.responseType = 'text' ;
		xhr.onreadystatechange = function() {
			if( xhr.readyState == 4 && xhr.status == 200 ) {
				if( xhr.response == '-1' ) {
					alert('失败');
				}
				else {
					onChangeBookShelf(Number(textfieldBookShelfLayerNumber.value));
				}
			}
		}
		xhr.onerror = function() {
			alert( 'Network error' );
		}
		xhr.send();
	}
	function onModifyBookShelfLayer() {
		var labelBookShelfId = document.getElementById( 'labelBookShelfId' ) ;
		var textfieldBookShelfLayerNumber = document.getElementById( 'textfieldBookShelfLayerNumber' ) ;
		var textfieldBookShelfLayerDesc = document.getElementById( 'textfieldBookShelfLayerDesc' ) ;
		var uri = '/book_shelf_layer_modifying.zweb?book_shelf_id='+labelBookShelfId.textContent+'&book_shelf_layer_number='+textfieldBookShelfLayerNumber.value+'&book_shelf_layer_desc='+encodeURIComponent(textfieldBookShelfLayerDesc.value) ;
		
		var xhr = new XMLHttpRequest() ;
		xhr.open( 'POST' , uri , true );
		xhr.responseType = 'text' ;
		xhr.onreadystatechange = function() {
			if( xhr.readyState == 4 && xhr.status == 200 ) {
				if( xhr.response == '-1' )
					alert('失败');
				else
					onChangeBookShelf(Number(textfieldBookShelfLayerNumber.value));
			}
		}
		xhr.onerror = function() {
			alert( 'Network error' );
		}
		xhr.send();
	}
	function onRemoveBookShelfLayer() {
		if( ! confirm('确认删除该书架层信息？') )
			return;
		
		var labelBookShelfId = document.getElementById( 'labelBookShelfId' ) ;
		var textfieldBookShelfLayerNumber = document.getElementById( 'textfieldBookShelfLayerNumber' ) ;
		var uri = '/book_shelf_layer_removing.zweb?book_shelf_id='+labelBookShelfId.textContent+'&book_shelf_layer_number='+textfieldBookShelfLayerNumber.value ;
		
		var xhr = new XMLHttpRequest() ;
		xhr.open( 'POST' , uri , true );
		xhr.responseType = 'text' ;
		xhr.onreadystatechange = function() {
			if( xhr.readyState == 4 && xhr.status == 200 ) {
				if( xhr.response == '-1' )
					alert('失败');
				else
					onLoadBookShelf(0);
			}
		}
		xhr.onerror = function() {
			alert( 'Network error' );
		}
		xhr.send();
	}
</script>
</body>
</html>
